package com.tjetc.usermanage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

import com.tjetc.domian.Communityevent;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.CommunityeventService;
import com.tjetc.vo.Result;

public class EventView {
    private Scanner scanner = new Scanner(System.in);
    private CommunityeventService communityeventService= ClientProxy.getProxyInstance(CommunityeventService.class);
    public static String current_user;
    public void start() {
        while (true) {
            System.out.println("活动菜单");
            System.out.println("1.查询活动信息");
            System.out.println("2.报名活动");
            System.out.println("3.取消报名");
            System.out.println("4.按活动名称搜索");
            System.out.println("5.按日期查询活动");
            System.out.println("6.按时间段查询活动");
            System.out.println("7.按地点查询活动");
            System.out.println("8.查看我报名的活动");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    select();
                    break;
                case 2:
                    join();
                    break;
                case 3:
                    cancel();
                    break;
                case 4:
                    searchByEventName();
                    break;
                case 5:
                    searchByEventDate();
                    break;
                case 6:
                    searchByDateRange();
                    break;
                case 7:
                    searchByLocation();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }

    private void cancel() {
        System.out.println("请输入活动id");
        int eventId = scanner.nextInt();
        Result result = communityeventService.cancel(eventId,current_user);
        System.out.println(result.getMsg());

    }

    private void join() {
        System.out.println("请输入活动id");
        int eventId = scanner.nextInt();
        String name=current_user;
        Result result = communityeventService.join(eventId,name);
        System.out.println(result.getMsg());

    }

    private void select() {
        List<Communityevent> list = communityeventService.list();
        if ( list.isEmpty()){
            System.out.println("暂无活动信息");
        }else {
            for (Communityevent communityevent : list) {
                System.out.println(communityevent);
            }
        }
    }

    private void searchByEventName() {
        System.out.println("请输入要搜索的活动名称（支持模糊搜索）：");
        String eventName = scanner.next();
        List<Communityevent> events = communityeventService.findByEventNameLike(eventName);
        if (events == null || events.isEmpty()) {
            System.out.println("未找到匹配的活动信息");
        } else {
            System.out.println("查询结果：");
            for (Communityevent event : events) {
                System.out.println(event);
            }
        }
    }

    private void searchByEventDate() {
        System.out.println("请输入要查询的日期（格式：yyyy-MM-dd）：");
        String dateStr = scanner.next();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.parse(dateStr); // 验证日期格式
            List<Communityevent> events = communityeventService.findByEventDate(dateStr);
            if (events == null || events.isEmpty()) {
                System.out.println("未找到该日期的活动信息");
            } else {
                System.out.println("查询结果：");
                for (Communityevent event : events) {
                    System.out.println(event);
                }
            }
        } catch (ParseException e) {
            System.out.println("日期格式错误，请使用yyyy-MM-dd格式");
        }
    }

    private void searchByDateRange() {
        System.out.println("请输入开始日期（格式：yyyy-MM-dd）：");
        String startDate = scanner.next();
        System.out.println("请输入结束日期（格式：yyyy-MM-dd）：");
        String endDate = scanner.next();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.parse(startDate); // 验证日期格式
            dateFormat.parse(endDate); // 验证日期格式
            List<Communityevent> events = communityeventService.findByDateRange(startDate, endDate);
            if (events == null || events.isEmpty()) {
                System.out.println("未找到该时间段的活动信息");
            } else {
                System.out.println("查询结果：");
                for (Communityevent event : events) {
                    System.out.println(event);
                }
            }
        } catch (ParseException e) {
            System.out.println("日期格式错误，请使用yyyy-MM-dd格式");
        }
    }

    private void searchByLocation() {
        System.out.println("请输入要搜索的活动地点（支持模糊搜索）：");
        String location = scanner.next();
        List<Communityevent> events = communityeventService.findByLocationLike(location);
        if (events == null || events.isEmpty()) {
            System.out.println("未找到匹配的活动信息");
        } else {
            System.out.println("查询结果：");
            for (Communityevent event : events) {
                System.out.println(event);
            }
        }
    }

}
